package com.ate.ui;

import androidx.annotation.RequiresApi;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.ate.MyApp;
import com.ate.bean.BuyCarBean;
import com.ate.gen.DaoManager;
import com.ate.gen.DaoSession;
import com.ate.javamvp.R;
import com.ate.bean.GoodsBean;
import com.ate.lib_core.http.mvp.view.BaseActity;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.List;

public class GoodsActivity extends BaseActity {
    private LinearLayout goodtop;
    private RelativeLayout titleRela;
    private ImageView back;
    private TextView details;
    private ScrollView scrollView;
    private TextView more;
    private com.youth.banner.Banner goodImgsBanner;
    private List<String>  list_banner;
    private GoodsBean.DataBean item;
    private Button add2buycar;
    private Bundle bundle;
    private MyApp myApp;

    @Override
    public int bandlayout() {
        return R.layout.activity_goods;
    }

    @Override
    public void initView() {
        add2buycar = (Button) findViewById(R.id.add2buycar);
        scrollView = findViewById(R.id.scroll);
        goodtop = findViewById(R.id.goodtop);
        titleRela = (RelativeLayout) findViewById(R.id.title_rela);
        back = (ImageView) findViewById(R.id.back);
        details = (TextView) findViewById(R.id.details);
        more = (TextView) findViewById(R.id.more);
        goodImgsBanner = (Banner) findViewById(R.id.good_imgs_banner);

    }

    private void addpopuwindow() {
        PopupWindow window = new PopupWindow(this);
        window.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        window.setHeight(400);
//        window.showAtLocation( Gravity.BOTTOM,0,0);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void initData() {
        Intent intent = getIntent();
        bundle = intent.getBundleExtra("item");
        item = (GoodsBean.DataBean) bundle.getSerializable("item");
        scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                if (scrollY <= 400 && scrollY > 0) {
                    titlehide(scrollY);
                }
            }
        });
        addBanner();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        add2buycar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DaoManager daoManager = DaoManager.getInstance();
                daoManager.init(GoodsActivity.this);
                DaoSession daoSession = daoManager.getDaoSession();
                daoSession.insert(new BuyCarBean(null,false,1,item.getTitle(),item.getPictUrl(),item.getReservePrice(),""));

                daoManager.closeDaoSession();
                daoManager.closeHelper();
                ToastUtils.showShort("添加成功，请到购物车查看");
            }
        });
    }

    private void addBanner() {

        List<String> images = item.getSmallImages();
        goodImgsBanner.setImages(images);
        goodImgsBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(context).load((String) path).into(imageView);
            }
        });
        goodImgsBanner.start();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);


    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void titlehide(int verticalOffset) {

        titleRela.setAlpha((float) verticalOffset / (float) 400.0);
        details.setAlpha((float) verticalOffset / (float) 400.0);


    }
}